#!/usr/bin/env python
import xmpp, sys, time
import urllib2
import json

def sendsubscriber(alarm_id, timestamp, prefix_dec, new_homeas, old_homeas,
    bad_path, urls):
    json_dict = {
      "timestamp": timestamp,
      "prefix": prefix_dec,
      "origin": (old_homeas != "0" and old_homeas) or "",
      "bad_origin": (new_homeas != "0" and new_homeas) or "",
      "bad_path_segment": bad_path,
      "uri": (
        "/fingerprints/%s/" % alarm_id),
    }
    # convert json_dict to JSON
    json_data = json.dumps(json_dict)
    # convert str to bytes (ensure encoding is OK)
    post_data = json_data.encode('utf-8')
    print >> sys.stderr, "[post_data] ", post_data # TODO
    # we should also say the JSON content type header
    headers = {'Content-Type': 'application/json', }
    url_array = urls.split(' ')
    for url in url_array:
        try:
            # now do the request for a url
            req = urllib2.Request(url, post_data, headers)
            # send the request
            res = urllib2.urlopen(req)
            # res is a file-like object
            response = res.read()
            res.close()
            print >> sys.stderr, "[response] ", response # TODO
        except Exception as e:
            print >> sys.stderr, "[exception] ", e

sendsubscriber(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5],
    sys.argv[6], sys.argv[7])
